package com.huawei.appmarket.support.thirdprovider.appvalidate;

import android.database.MatrixCursor;
import android.database.MatrixCursor.RowBuilder;
import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.appgallery.serverreqkit.api.listener.IServerCallBack;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import o.cfw;
import o.cvt;
import o.BaseRequest;
import o.Logger;
import o.CollectCheck;

public class AppValidateManager {
    private static final int DEFAULT_ROW_COUNTS = 1;
    private static final String IS_VALIDE = "isLegalApp";
    private static final String PKG = "package";
    private static final String TAG = "UrlRecognitionManager";
    private static final long WAIT_TIMEOUT_DFT = 5000;
    private final AtomicInteger mRequestSequence = new AtomicInteger(0);

    static class AppValidateStoreCallBack implements IServerCallBack {
        private TimeoutQueryRequest mRequestObj;

        public AppValidateStoreCallBack(TimeoutQueryRequest timeoutQueryRequest) {
            this.mRequestObj = timeoutQueryRequest;
        }

        public void prePostResult(BaseRequest o_cvu, ResponseBean responseBean) {
        }

        public void notifyResult(BaseRequest o_cvu, ResponseBean responseBean) {
            if (this.mRequestObj.isExpired()) {
                Logger.ˎ(AppValidateManager.TAG, "onResult: [" + this.mRequestObj.getRequestSequence() + "]" + ", Request is expired ,skip");
                this.mRequestObj.setResult(null);
                this.mRequestObj.releaseLock();
            } else if (responseBean.getRtnCode_() == 0 && responseBean.getResponseCode() == 0) {
                AppValidateRes appValidateRes = (AppValidateRes) responseBean;
                if (CollectCheck.isEmpty(appValidateRes.list_)) {
                    Logger.e(AppValidateManager.TAG, "res.list_ length is 0.");
                }
                MatrixCursor matrixCursor = new MatrixCursor(new String[]{"package", AppValidateManager.IS_VALIDE}, 1);
                for (int i = 0; i < appValidateRes.list_.size(); i++) {
                    ValidateResult validateResult = (ValidateResult) appValidateRes.list_.get(i);
                    RowBuilder newRow = matrixCursor.newRow();
                    newRow.add(validateResult.pkg_);
                    newRow.add(Integer.valueOf(validateResult.isLegal_));
                    if (Logger.ˎ()) {
                        Logger.ˏ(AppValidateManager.TAG, "package=" + validateResult.pkg_ + " isLeageal=" + validateResult.isLegal_);
                    }
                }
                this.mRequestObj.setResult(matrixCursor);
                this.mRequestObj.releaseLock();
            } else {
                this.mRequestObj.setResult(null);
                this.mRequestObj.releaseLock();
                Logger.e(AppValidateManager.TAG, "res.rtnCode_=" + responseBean.getRtnCode_() + "  res.responseCode=" + responseBean.getResponseCode());
            }
        }
    }

    static class TimeoutQueryRequest {
        AtomicBoolean mExpired = new AtomicBoolean(false);
        Object mLock = new Object();
        int mRequestSequence = 0;
        MatrixCursor mResult = null;

        public TimeoutQueryRequest(int i) {
            this.mRequestSequence = i;
        }

        public int getRequestSequence() {
            return this.mRequestSequence;
        }

        public synchronized MatrixCursor getResult() {
            return this.mResult;
        }

        public synchronized void setResult(MatrixCursor matrixCursor) {
            this.mResult = matrixCursor;
        }

        public void waitRequestResult(long j) {
            synchronized (this.mLock) {
                long currentTimeMillis;
                do {
                    try {
                        if (getResult() == null) {
                            Logger.ˎ(AppValidateManager.TAG, "waitRequestResult: [" + this.mRequestSequence + "], timeout = " + j);
                            currentTimeMillis = System.currentTimeMillis();
                            this.mLock.wait(j);
                        }
                    } catch (InterruptedException e) {
                        Logger.ˎ(AppValidateManager.TAG, "InterruptedException, [" + this.mRequestSequence + "]");
                    }
                    break;
                } while (System.currentTimeMillis() - currentTimeMillis < j);
                this.mExpired.set(true);
            }
        }

        public void releaseLock() {
            synchronized (this.mLock) {
                this.mLock.notifyAll();
            }
        }

        public boolean isExpired() {
            return this.mExpired.get();
        }
    }

    public MatrixCursor getAppCheckInfo(String[] strArr, String[] strArr2) {
        return getAppCheckInfo(WAIT_TIMEOUT_DFT, strArr, strArr2);
    }

    public MatrixCursor getAppCheckInfo(long j, String[] strArr, String[] strArr2) {
        int nextRequestSequence = getNextRequestSequence();
        Logger.ˎ(TAG, "getUrlCheckInfo: Starts, [" + nextRequestSequence + "]");
        TimeoutQueryRequest timeoutQueryRequest = new TimeoutQueryRequest(nextRequestSequence);
        doCheck(timeoutQueryRequest, strArr, strArr2);
        timeoutQueryRequest.waitRequestResult(j);
        Logger.ˎ(TAG, "getUrlCheckInfo: Ends, [" + nextRequestSequence + "]");
        return timeoutQueryRequest.getResult();
    }

    private int getNextRequestSequence() {
        return this.mRequestSequence.incrementAndGet();
    }

    private void doCheck(TimeoutQueryRequest timeoutQueryRequest, String[] strArr, String[] strArr2) {
        cvt newInstance = AppValidateReq.newInstance();
        List arrayList = new ArrayList();
        if (Logger.ˎ()) {
            Logger.ˏ(TAG, "package length=" + strArr.length);
        }
        for (int i = 0; i < strArr.length; i++) {
            arrayList.add(new CheckAppInfo(strArr[i], strArr2[i]));
        }
        AppListInfo appListInfo = new AppListInfo();
        appListInfo.appList_ = arrayList;
        newInstance.appList_ = appListInfo;
        cfw.ˎ(newInstance, new AppValidateStoreCallBack(timeoutQueryRequest));
    }
}
